﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.GFT
{
    [Description("GFT:生产入库汇总查询"), HotUpdate]
    public class szrkhzcx : AbstractDynamicFormPlugIn
    {
        public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
        {
            base.AfterBarItemClick(e);
            switch (e.BarItemKey.ToString())
            {
                case "tbFresh":
                    {
                        StringBuilder sb = new StringBuilder();
                        string tpartno = this.Model.GetValue("FMaterialId") != null ? ((DynamicObject)this.Model.GetValue("FMaterialId"))[1].ToString() : "0";
                        string orgid = this.Model.GetValue("F_PAEZ_OrgId") != null ? ((DynamicObject)this.Model.GetValue("F_PAEZ_OrgId"))[0].ToString() : "0";
                        string bgndate = this.Model.GetValue("FBgnDate") != null ? this.Model.GetValue("FBgnDate").ToString() : "";
                        string enddate = this.Model.GetValue("FEndDate") != null ? this.Model.GetValue("FEndDate").ToString() : "";
                        string bgnno = this.Model.GetValue("FBgnNo") != null ? this.Model.GetValue("FBgnNo").ToString() : "";
                        string sql = string.Format("/*dialect*/ execute yls_scykhz {0},{1},'{2}','{3}','{4}'", tpartno, orgid, bgndate, enddate, bgnno);
                        //Double ordertotal = 0, intotal = 0, returntotal = 0, followtotal = 0, badtotal = 0,tinqty=0,rectotal=0;
                        Entity entity = (Entity)this.View.Model.BusinessInfo.GetEntryEntity("FEntity");
                        DynamicObjectCollection dyEntity = this.View.Model.GetEntityDataObject(entity);
                        this.Model.BeginIniti();
                        this.Model.DeleteEntryData("FEntity");
                        int i = 1;
                        using (IDataReader rd = DBServiceHelper.ExecuteReader(this.Context, sql))
                        {
                            while (rd.Read())
                            {
                                DynamicObject newRow = new DynamicObject(dyEntity.DynamicCollectionItemPropertyType);
                                newRow = new DynamicObject(dyEntity.DynamicCollectionItemPropertyType);
                                newRow["Seq"] = i;
                                newRow["FMono"] = rd["fmono"];
                                newRow["FDeptId_Id"] = rd["fdept"];
                                newRow["FTMaterialId_Id"] = rd["fmaterialid"];
                                newRow["FOrderQty"] = rd["fmoqty"];
                                //ordertotal += Convert.ToDouble(rd["fmoqty"]);
                                newRow["FFollowQty"] = rd["ffollowqty"];
                                //followtotal += Convert.ToDouble(rd["ffollowqty"]);
                                newRow["FToNextQty"] = rd["frecqty"];
                                //rectotal += Convert.ToDouble(rd["frecqty"]);
                                newRow["FThisInQty"] = rd["finqty"];
                                //intotal += Convert.ToDouble(rd["finqty"]);
                                newRow["FThisReturnQty"] = rd["fstorereturnqty"];
                                //returntotal += Convert.ToDouble(rd["fstorereturnqty"]);

                                newRow["FThisTInQty"] = rd["ftinqty"];
                                //tinqty += Convert.ToDouble(rd["ftinqty"]);
                                newRow["FBadQty"] = rd["fbadqty"];
                                //badtotal += Convert.ToDouble(rd["fbadqty"]);
                                i += 1;
                                dyEntity.Add(newRow);
                            }

                            //取消汇总
                            //if (i > 1)
                            //{
                            //    DynamicObject newRow = new DynamicObject(dyEntity.DynamicCollectionItemPropertyType);
                            //    newRow = new DynamicObject(dyEntity.DynamicCollectionItemPropertyType);
                            //    newRow["Seq"] = i;
                            //    newRow["FOrderQty"] = ordertotal;
                            //    newRow["FFollowQty"] = followtotal;
                            //    newRow["FThisInQty"] = intotal;
                            //    newRow["FThisReturnQty"] = returntotal;
                            //    newRow["FThisTInQty"] = tinqty;
                            //    newRow["FBadQty"] = badtotal;
                            //    newRow["FToNextQty"] = rectotal;                              
                            //    dyEntity.Add(newRow);
                            //}
                        }
                        DBServiceHelper.LoadReferenceObject(this.Context, dyEntity.ToArray(), dyEntity.DynamicCollectionItemPropertyType, false);
                        this.Model.EndIniti();


                        //取消汇总
                        //i = this.Model.GetEntryRowCount("FEntity")-1;
                        //EntryGrid grid = this.View.GetControl<EntryGrid>("FEntity");
                        //grid.SetRowBackcolor("#FFFF00", i);
                        //grid.Enabled = false;
                        this.View.UpdateView("FEntity");

                        break;
                    }

            }
        }
    }
}
